package parse_and_index.search.inverted;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;

import parse_and_index.Util;
import parse_and_index.build.inverted.InvertedIndex;

public class SearchInverted {

	private static final String	CONSOLE	= "-console";

	private static boolean		toConsole	= false;

	public static void run(final String[] args) {
		String indexFile = "";
		String outputFilename = "";
		String searchToken = "";
		final int testNumberOfWords = 1;

		for (int i = 0; i < args.length; i++) {
			i++;
			if (i >= args.length) {
				Util.printUsage();
			}
			indexFile = args[i];
			System.out.println("indexFile: " + indexFile);
			i++;
			if (i >= args.length) {
				Util.printUsage();
			}
			searchToken = args[i];
			System.out.println("searchToken: " + searchToken);
			i++;
			if (i >= args.length) {
				Util.printUsage();
			}
			if (args[i].equals(CONSOLE)) {
				toConsole = true;
			}
			if (toConsole == false) {
				outputFilename = args[i];
				System.out.println("outputFilename: " + outputFilename);
				i++;
				if (i >= args.length) {
					Util.printUsage();
				}
			}
		}
		try {
			InvertedIndex invertedIndex = InvertedIndex.getIndex();
			invertedIndex = invertedIndex.read(indexFile);
			invertedIndex.getInfo();
			long t1 = 0, t2 = 0;
			final long t = 0, min = 1000, max = 0;
			int cnt = 0;
			ArrayList<Result> results = null;

			final String key = readKeyFromFile(searchToken);
			final Query q = new Query(key);
			t1 = System.currentTimeMillis();
			results = invertedIndex.search(q);
			t2 = System.currentTimeMillis();
			for (final Result result : results) {
				cnt += result.getPages().size();
			}
			System.out.println("Pogodaka(strane): " + cnt);

			printResults(outputFilename, t1, t2, results);
		}
		catch (final Exception ex) {
			ex.printStackTrace();
		}
	}

	public static void printResults(final String outputFilename, final long t1, final long t2, final ArrayList<Result> results) {
		if (toConsole) {
			for (final Result result : results) {
				System.out.println(result);
			}
			System.out.println("Vreme pretrage: " + (t2 - t1) + "ms");
		}
		else {
			OutputStreamWriter output = null;
			try {
				output = new OutputStreamWriter(new FileOutputStream(outputFilename));
			}
			catch (final Exception e) {
				System.out.println("Can't open output file.");
				e.printStackTrace();
			}
			try {
				output.append("Vreme pretrage: " + (t2 - t1) + "ms\n");
				for (int i = 0; i < results.size(); i++) {
					output.append(results.get(i).toString() + "\n");
				}
				output.flush();
				output.close();
			}
			catch (final Exception e) {
				System.out.println("Can't write output file.");
				e.printStackTrace();
			}
		}
	}

	public static String readKeyFromFile(final String filename) {
		try {
			final FileInputStream fstream = new FileInputStream(filename);
			final DataInputStream in = new DataInputStream(fstream);
			final BufferedReader br = new BufferedReader(new InputStreamReader(in, "utf-8"));

			String strLine = "", key = "";
			while ((strLine = br.readLine()) != null) {
				key = strLine;
			}
			br.close();
			in.close();
			fstream.close();
			return key;
		}
		catch (final Exception e) {
			System.out.println("Can't read file.");
			e.printStackTrace();
		}
		return null;
	}
}